package com.pricl.biz.upload.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springside.modules.web.MediaTypes;

import com.pricl.biz.file.entity.FileManager;
import com.pricl.biz.file.entity.criteria.FileManagerCriteria;
import com.pricl.biz.file.service.FileManagerService;
import com.pricl.biz.upload.FileItem;
import com.pricl.biz.upload.UploadFileManager;
import com.pricl.biz.upload.exception.UnsupportedFileLengthException;
import com.pricl.biz.upload.exception.UnsupportedFormatException;
import com.pricl.biz.upload.manager.SystemConfigManager;
import com.pricl.frame.common.web.rest.RestException;
import com.pricl.frame.common.web.rest.RestResponse;
import com.pricl.frame.common.web.rest.RestResponseBuilder;
import com.pricl.frame.common.web.rest.RestResponseCode;


/**
 * <ol>
 * <li>文件管理上传接口</li> 
 * <ol>
 *     
 * @author jackzhong
 * @since 2014年10月31日下午9:51:09
 */
@Controller
@RequestMapping("/upload")
public class UploadFileManagerController {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	

	/**
	 * 图片上传管理类
	 */
	@Resource(name="imageUploadFileManager")
	private UploadFileManager imageUploadFileManager;
	
	/**
	 * 视频上传管理类
	 */
	@Resource(name="videoUploadFileManager")
	private UploadFileManager videoUploadFileManager;

	/**
	 * 文档上传管理类
	 */
	@Resource(name="docUploadFileManager")
	private UploadFileManager docUploadFileManager;

	/**
	 * 压缩包上传管理类
	 */
	@Resource(name="zipUploadFileManager")
	private UploadFileManager zipUploadFileManager;

	/**
	 * 帮助文档上传管理类
	 */
	@Resource(name="helpUploadFileManager")
	private UploadFileManager helpUploadFileManager;

	/**
	 * 音频上传管理类
	 */
	@Resource(name="audioUploadFileManager")
	private UploadFileManager audioUploadFileManager;

	@Autowired
	private FileManagerService fileManagerService;

	//@Resource(name = "FileTotalInfoService")
	//private FileTotalInfoService fileTotalInfoService;

	@Resource(name="systemConfigManager")
	protected SystemConfigManager systemConfigManager;

	/**
	 * 图片上传接口
	 * 
	 * @return 
	 */
	@RequestMapping(value = "/picture", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> picture(MultipartHttpServletRequest request, HttpServletResponse response) {
		try {
			List<FileItem> fileItems = imageUploadFileManager.handlerUpload(request);
			return new RestResponseBuilder("文件上传成功!").setResultData(fileItems).builderResponseEntity();
		} catch (UnsupportedFormatException ex) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件格式不符以下类型：" + imageUploadFileManager.getAllowFileExtensions()).builderResponseEntity();
		} catch (UnsupportedFileLengthException e) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件大小超出设置的值：" + imageUploadFileManager.getAllowFileSize()).builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件上传失败,请联系管理员!", e);
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(), "文件上传失败,文件格式不支持。")
					.builderResponseEntity();
		}
	}

	/**
	 * 视频上传接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/video", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> video(MultipartHttpServletRequest request, HttpServletResponse response) {
		try {
			List<FileItem> fileItems = videoUploadFileManager.handlerUpload(request);
			return new RestResponseBuilder("文件上传成功!").setResultData(fileItems).builderResponseEntity();
		} catch (UnsupportedFormatException ex) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件格式不符以下类型：" + videoUploadFileManager.getAllowFileExtensions()).builderResponseEntity();
		} catch (UnsupportedFileLengthException e) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件大小超出设置的值：" + videoUploadFileManager.getAllowFileSize()).builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件上传失败,请联系管理员!", e);
			throw new RestException("文件上传失败,请联系管理员!");
		}
	}

	/**
	 * 文档上传接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doc", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> doc(MultipartHttpServletRequest request, HttpServletResponse response) {
		try {
			List<FileItem> fileItems = docUploadFileManager.handlerUpload(request);
			return new RestResponseBuilder("文件上传成功!").setResultData(fileItems).builderResponseEntity();
		} catch (UnsupportedFormatException ex) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件格式不符以下类型：" + docUploadFileManager.getAllowFileExtensions()).builderResponseEntity();
		} catch (UnsupportedFileLengthException e) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件大小超出设置的值：" + docUploadFileManager.getAllowFileSize()).builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件上传失败,请联系管理员!", e);
			throw new RestException("文件上传失败,请联系管理员!");
		}
	}

	/**
	 * "压缩包上传接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/zip", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> zip(MultipartHttpServletRequest request, HttpServletResponse response) {
		try {
			List<FileItem> fileItems = zipUploadFileManager.handlerUpload(request);
			return new RestResponseBuilder("文件上传成功!").setResultData(fileItems).builderResponseEntity();
		} catch (UnsupportedFormatException ex) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件格式不符以下类型：" + zipUploadFileManager.getAllowFileExtensions()).builderResponseEntity();
		} catch (UnsupportedFileLengthException e) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件大小超出设置的值：" + zipUploadFileManager.getAllowFileSize()).builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件上传失败,请联系管理员!", e);
			throw new RestException("文件上传失败,请联系管理员!");
		}
	}

	/**
	 * 帮助文档
	 * 
	 * @return
	 */
	@RequestMapping(value = "/help", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> help(MultipartHttpServletRequest request, HttpServletResponse response) {
		System.out.println("进入");
		try {
			List<FileItem> fileItems = helpUploadFileManager.handlerUpload(request);
			return new RestResponseBuilder("文件上传成功!").setResultData(fileItems).builderResponseEntity();
		} catch (UnsupportedFormatException ex) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件格式不符以下类型：" + helpUploadFileManager.getAllowFileExtensions()).builderResponseEntity();
		} catch (UnsupportedFileLengthException e) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件大小超出设置的值：" + helpUploadFileManager.getAllowFileSize()).builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件上传失败,请联系管理员!", e);
			throw new RestException("文件上传失败,请联系管理员!");
		}
	}

	/**
	 * 音频上传接口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/audio", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> audio(MultipartHttpServletRequest request, HttpServletResponse response) {
		try {
			List<FileItem> fileItems = audioUploadFileManager.handlerUpload(request);
			return new RestResponseBuilder("文件上传成功!").setResultData(fileItems).builderResponseEntity();
		} catch (UnsupportedFormatException ex) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件格式不符以下类型：" + audioUploadFileManager.getAllowFileExtensions()).builderResponseEntity();
		} catch (UnsupportedFileLengthException e) {
			return new RestResponseBuilder(RestResponseCode.getFailureResponseCode(),
					"文件上传失败,文件大小超出设置的值：" + audioUploadFileManager.getAllowFileSize()).builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件上传失败,请联系管理员!", e);
			throw new RestException("文件上传失败,请联系管理员!");
		}
	}

	/**
	 * 删除上传的文件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/delete/{sid}", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> deleteBySid(@PathVariable("sid") Long sid, HttpServletResponse response) {
		try {
			FileManager fileManager = this.fileManagerService.selectByPrimaryKey(sid);
			if (fileManager != null) {
				fileManager.setBusinessSid(null);
			}
			this.fileManagerService.updateByPrimaryKey(fileManager);
			return new RestResponseBuilder("文件删除成功!").builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件删除失败,请联系管理员!", e);
			throw new RestException("文件删除,请联系管理员!");
		}
	}

	/**
	 * 删除上传的文件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteFileUrl", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseEntity<RestResponse> deleteByFileUrl(HttpServletRequest request, HttpServletResponse response) {
		try {
			FileManagerCriteria fileManagerCriteria = new FileManagerCriteria();
			String fileUrl = request.getParameter("fileUrl");
			fileManagerCriteria.setFileUrlEqualTo(fileUrl);
			List<FileManager> fileManagerList = fileManagerService.selectByEntity(fileManagerCriteria);

			if (fileManagerList != null) {
				for (FileManager fileManager : fileManagerList) {

					this.fileManagerService.deleteByPrimaryKey(fileManager.getId());
				}
			}
			return new RestResponseBuilder("文件删除成功!").builderResponseEntity();
		} catch (Exception e) {
			logger.error("文件删除失败,请联系管理员!", e);
			throw new RestException("文件删除,请联系管理员!");
		}
	}

	

}
